package game;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Point;
import java.awt.geom.AffineTransform;
import java.awt.geom.Point2D;
import java.awt.image.BufferedImage;

public class GraphicUtil
{
    public static void clearScreen(Graphics2D g, Dimension size)
    {
        Color color = g.getColor();
        g.setColor(Color.WHITE);
        g.fillRect(0, 0, size.width, size.height);
        g.setColor(color);
    }
    
    public static void renderCenterText(Graphics g, String s, int fontSize, Dimension maxXY)
    {
        g.setFont(new Font("Monospaced", Font.PLAIN, fontSize));
        FontMetrics fm = g.getFontMetrics();
        int startX = maxXY.width  / 2 - fm.stringWidth(s) / 2;
        int startY = maxXY.height / 2 + fm.getDescent();
        g.setColor(Color.BLACK);
        g.drawString(s, startX, startY);
    }
    
    public static BufferedImage rotateImage(BufferedImage inputImage, int angle)
    {
        int X, Y = 0;
        if ((angle == 0) || (angle == 180) || (angle == 360))
        {
            X = inputImage.getWidth(null);
            Y = inputImage.getHeight(null);
        }
        else
        {
            X = inputImage.getHeight(null);
            Y = inputImage.getWidth(null);
        }
        BufferedImage sourceBI = new BufferedImage(X, Y, BufferedImage.TYPE_INT_RGB);
        AffineTransform at = new AffineTransform();
        // rotate around image center
        at.rotate(Math.toRadians(angle), (sourceBI.getWidth() / 2), (sourceBI.getHeight() / 2));
        /*
         * translate to make sure the rotation doesn't cut off any image data
         */
        AffineTransform translationTransform;
        translationTransform = findTranslation(at, sourceBI, angle);
        at.preConcatenate(translationTransform);
        // draw
        Graphics2D g = (Graphics2D) sourceBI.getGraphics();
        g.setTransform(at);
        g.drawImage(inputImage, 0, 0, null);
        return sourceBI;
    }
    
    /*
     * find proper translations to keep rotated image correctly displayed
     */
    private static AffineTransform findTranslation(AffineTransform at, BufferedImage bi, int angle)
    {
        Point2D p2din, p2dout;
        double ytrans, xtrans = 0.0;
        AffineTransform tat = new AffineTransform();
        if (angle == 180)
        {
            p2din = new Point2D.Double(0, bi.getHeight());
        }
        else
        {
            p2din = new Point2D.Double(0.0, 0.0);
        }
        p2dout = at.transform(p2din, null);
        if (angle == 270)
        {
            xtrans = p2dout.getX();
            ytrans = xtrans;
        }
        else
        {
            ytrans = p2dout.getY();
            xtrans = ytrans;
        }
        tat.translate(-xtrans, -ytrans);
        return tat;
    }
    
    public static void fillRect(Graphics g, Point position, Dimension size, Color color)
    {
        g.setColor(color);
        g.fillRect(position.x, position.y, size.width, size.height);
    }
    
    public static void drawRect(Graphics g, Point position, Dimension size, Color color)
    {
        g.setColor(color);
        g.drawRect(position.x, position.y, size.width, size.height);
    }
    
    public static void drawImage(Graphics g, Point position, BufferedImage img)
    {
        drawImage(g, position, img, 0);
    }
    
    public static void drawImage(Graphics g, Point position, BufferedImage img, int border)
    {
        g.drawImage(img, (position.x * img.getWidth()) + border, (position.y * img.getHeight()) + border, null);
    }
}
